package com.pulse.base.task;

import com.pulse.base.domain.CircuitMeasure;
import com.pulse.base.service.ICircuitMeasureService;
import com.pulse.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("measureTestTask")
public class MeasureTestTask {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private ICircuitMeasureService circuitMeasureService;


    /**
     * 周期测试定时任务
     *
     * @param deviceNo
     * @param channel
     */
    public void periodicTesting(String deviceNo, Integer channel){
        // 将测试消息发送到缓存队列
        redisCache.push("jobQueue:"+deviceNo,deviceNo+"_"+channel+"_2_0");
    }

    /**
     * 光路测试状态监控
     */
    public void measureTestStatus(){
        long size = redisCache.qSize("MeasureTestList");
        if(size <= 0){
            return;
        }
        for(int i = 0; i < size; i++){
            Object mt = redisCache.pop("MeasureTestList");
            if(mt != null){
                String measureTest = mt.toString();
                String[] split = measureTest.split(":");
                if(split.length == 2){
                    Object o = redisCache.getCacheObject("CircuitMeasureBack:" + split[0]);
                    if(o != null){
                        CircuitMeasure circuitMeasure = (CircuitMeasure) o;
                        redisCache.push("MeasureTestList",measureTest);
                        continue;
                    }
                    CircuitMeasure cm = circuitMeasureService.selectCircuitMeasureByMeasureId(Long.parseLong(split[1]));
                    if(cm==null){
                        continue;
                    }
                    if(cm.getStatus().equals(1)){
                        cm.setStatus(11);
                        cm.setCircuitStatus(1);
                        cm.setRemark("长时间未收到设备返回的测试消息");
                        circuitMeasureService.updateCircuitMeasure(cm);
                    }
                }
            }
        }
    }
}
